<?php

namespace app\utils\ui\vue;

use app\utils\ui\UIView;
use app\utils\ui\VueView;
use Closure;
use Psr\Container\ContainerExceptionInterface;
use stdClass;

/**
 * VUE 基类 BASE，考虑是否需要更改 VueFormItem
 */
abstract class Base extends stdClass implements VueView{

    /**
     * @param array|Closure $attr
     * @param array|UIView $child
     */
    public function __construct($attr,$child)
    {
        $this->child=$child;
        if($attr instanceof Closure){
            $this->attr = $attr($this);
        }else{
            $this->attr=$attr;
        }
    }

    /**
     * @var array|UIView
     */
    public $child;
    /**
     * @var array|Closure
     */
    public $attr;

    public function base_render($tag)
    {
        $attr_text='';
        foreach($this->attr as $name=>$value){
            if($value instanceof VueVarValue){
                $this->var[$value->var]=$value->val;
                $attr_text.=$name.'="'.$value->var.'" ';
                continue;
            }
            if($value instanceof VueMethod){
                if($value->raw_name){
                    $attr_text.=$name.'="'.$value->method.'" ';
                }else{
                    $attr_text.='v-on:'.$name.'="'.$value->method.'" ';
                }
                continue;
            }

            //若key为数字，值为字符串，视为一个没有值的属性
            if(is_numeric($name) && is_string($value)){
                $attr_text.=$name.' ';
                continue;
            }

            if(is_array($value)){
                $attr_text.=$name.'="'.htmlspecialchars(json_encode($value,JSON_UNESCAPED_UNICODE)).'" ';
            }else{
                $attr_text.=$name.'="'.$value.'" ';
            }
        }

        $html_start = '<'.$tag.' '.$attr_text.'>';
        $html_end = '</'.$tag.'>';

        $html2='';
        if(is_array($this->child)){
            foreach($this->child as $child2){
                if($child2 instanceof UIView){
                    $html2.=$child2->render();
                }
            }
        }else{
            if($this->child){
                $html2=$this->child->render();
            }
        }

        return $html_start.PHP_EOL.$html2.PHP_EOL.$html_end.PHP_EOL;
    }
    
    public function data()
    {
        $ret=array();
        if($this->child){
            if(is_array($this->child)){
                $list = array();
                foreach($this->child as $child){
                    if($child instanceof UIView){
                        $list = array_merge($list,$child->data());
                    }
                }
                $list;
                $ret = array_merge($ret,$list);
            }else{
                $ret = array_merge($ret,$this->child->data());
            }
        }
        
        $var = array();
        foreach($this->attr as $value){
            if($value instanceof VueVarValue){
                $var[$value->var]=$value->val;
            }
        }
        if(count($var)>0){
            $ret = array_merge($ret,$var);
        }
        return $ret;
    }

    public function method()
    {
        $list = [];
        if(is_array($this->attr)){
            foreach($this->attr as $event_name=>$value){
                if($value instanceof VueMethod){
                    $list[]=array(
                        'event_name'=>$event_name,
                        'view'=>$this,
                        'method'=>$value->method
                    );
                }
            }
        }
        if(is_array($this->child)){
            foreach($this->child as $child){
                if($child instanceof VueView){
                    $list2 = $child->method();
                    foreach($list2 as $v){
                        $list[]=$v;
                    }
                }
            }
        }
        if($this->child instanceof VueView){
            $list2 = $this->child->method();
            foreach($list2 as $v){
                $list[]=$v;
            }
        }
        return $list;
    }
    
}